package com.example.wecompose.ui

import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.wecompose.R
import com.example.wecompose.WeViewModel
import com.example.wecompose.ui.theme.WeComposeTheme

@Composable
fun MeList(viewModel: WeViewModel) {

    MeListContent(viewModel)
}

@Composable
private fun MeListContent(viewModel: WeViewModel) {
    Column(
        Modifier
            .fillMaxSize()
            .background(WeComposeTheme.colors.background)
    ) {

        MeTopInfo(viewModel)
        Spacer(Modifier.height(8.dp))

        MeListItem("支付",R.drawable.ic_zf)
        Spacer(Modifier.height(8.dp))

        MeListItem("收藏",R.drawable.ic_sc)
        HorizontalDivider(Modifier.padding(start = 60.dp))
        MeListItem("朋友圈",R.drawable.ic_pyq)
        HorizontalDivider(Modifier.padding(start = 60.dp))
        MeListItem("卡包",R.drawable.ic_kb)
        HorizontalDivider(Modifier.padding(start = 60.dp))
        MeListItem("表情",R.drawable.ic_bq)
        HorizontalDivider(Modifier.padding(start = 60.dp))
        MeListItem("设置",R.drawable.ic_sz)


    }
}

@Composable
private fun MeListItem(title: String, @DrawableRes discoveryIcon: Int) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier
            .background(WeComposeTheme.colors.listItem)
            .fillMaxWidth()
            .padding(10.dp)
            .clickable() {

            }
    ) {
        Image(
            painterResource(discoveryIcon),
            null,
            Modifier
                .padding(4.dp)
                .size(24.dp)
                .clip(RoundedCornerShape(4.dp))
        )
        Spacer(Modifier.width(10.dp))

        Text(
            title, fontSize = 17.sp, color = WeComposeTheme.colors.textPrimary
        )

        Spacer(Modifier.weight(1f))

        Icon(
            painterResource(R.drawable.ic_arrow),
            null,
            Modifier
                .align(Alignment.CenterVertically)
                .size(36.dp)
                .padding(8.dp),
            tint = WeComposeTheme.colors.icon
        )

    }
}

@Composable
private fun MeTopInfo(viewModel: WeViewModel) {
    Row(
        Modifier
            .background(WeComposeTheme.colors.listItem)
            .fillMaxWidth()
            .padding(20.dp, 50.dp, 20.dp, 50.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Image(
            painterResource(viewModel.userInfo.avatar),
            null,
            Modifier
                .size(55.dp)
                .clip(RoundedCornerShape(8.dp))
        )

        Column(Modifier.weight(1f)) {
            Text(
                viewModel.userInfo.name,
                fontWeight = FontWeight.Bold,
                fontSize = 18.sp,
                color = WeComposeTheme.colors.textPrimaryMe,
                modifier = Modifier.padding(16.dp, 8.dp, 16.dp, 8.dp)
            )
            Text(
                "微信号:xxxxxxxx",
                fontSize = 12.sp,
                color = WeComposeTheme.colors.textSecondary,
                modifier = Modifier.padding(16.dp, 8.dp, 16.dp, 8.dp)
            )

            WeChatStatusTag(
                modifier = Modifier.padding(16.dp, 8.dp, 16.dp, 8.dp),
                onClick = { /* 处理点击事件 */ }
            )
        }

        Icon(
            painterResource(R.drawable.ic_qe_code),
            null,
            Modifier
                .align(Alignment.CenterVertically)
                .size(36.dp)
                .padding(8.dp),
            tint = WeComposeTheme.colors.icon
        )

        Icon(
            painterResource(R.drawable.ic_arrow),
            null,
            Modifier
                .align(Alignment.CenterVertically)
                .size(36.dp)
                .padding(8.dp),
            tint = WeComposeTheme.colors.icon
        )


    }
}

@Composable
fun WeChatStatusTag(modifier: Modifier = Modifier, onClick: () -> Unit = {}) {
    Surface(
        modifier = modifier
            .clip(RoundedCornerShape(16.dp))
            .clickable { onClick() }
            .border(
                width = 1.dp,
                color = WeComposeTheme.colors.textSecondary,
                shape = RoundedCornerShape(16.dp)
            ),
        color = Color.Transparent // 透明背景
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.Center,
            modifier = Modifier.padding(horizontal = 12.dp, vertical = 6.dp)
        ) {
            Text(
                text = "+",
                color = WeComposeTheme.colors.textSecondary,
                fontSize = 18.sp,
                fontWeight = FontWeight.Bold,
                modifier = Modifier.padding(end = 4.dp)
            )
            Text(
                text = "状态",
                color = WeComposeTheme.colors.textSecondary,
                fontSize = 14.sp
            )
        }
    }
}

@Preview
@Composable
private fun MeListPreview() {
    MeListContent(viewModel())
}